layui.define(['form','table', 'layer', 'baseutil', 'dialog', 'validUtil'], function (exports) {
    var form = layui.form,
        $ = layui.jquery,
        validUtil = layui.validUtil,
        table = layui.table,
        dialog = layui.dialog,
        baseutil = layui.baseutil;
    //获取当前iframe的name值
    var iframeObj = $(window.frameElement).attr('name');
    var page = {
        /***
         * @Author: YuShengQiang
         * @Date: 2018-11-27 11:16
         * @Description:初始化列表数据
         * @param cols 必填 展示列表参数
         * @param url 必填 数据接口URL
         * @param isPage 选填 默认是分页
         */
        initDate: function (cols, url, isPage) {
            if (isPage != "false" && isPage != false) {
                isPage = true;//默认分页
            }
            var tableIns = table.render({
                elem: '#entityList',
                url: url,
                cellMinWidth: 85,
                page: isPage,
                //height: "full-125",
                height:471,
                limit: 10,
                method: 'post',
                limits: [10, 20, 50, 100,200],
                id: "entityList",
                cols: [cols]
            });
            return tableIns;
        },

        newInitDate: function (id,cols, url, isPage) {
            if (isPage != "false" && isPage != false) {
                isPage = true;//默认分页
            }
            var tableIns = table.render({
                elem: ("#"+id),
                url: url,
                cellMinWidth: 85,
                page: isPage,
               height: "full-280",
                limit: 10,
                method: 'post',
                limits: [10, 20, 50, 100,200],
                id: id,
                cols: [cols]
            });
            return tableIns;
        },

        /**
         * @Author: YuShengQiang
         * @Date: 2018-11-27 11:16
         * @Description: 字典码获取
         * @param cols 必填 字典码code，多个以“，”隔开
         * @param successFn 必填 调用成功回调方法
         */
        initDict: function (params, successFn) {
            baseutil.post("/sysDictContent/listByCode", {
                dictTypes: params
            }, function (result) {
                if (0 == result.code) {
                    //成功
                    successFn(result.data);
                } else {
                    top.layer.msg(result.msg);
                }
            });
        },
        /**
         * @Author: YuShengQiang
         * @Date: 2018-11-27 11:16
         * @Description: 特殊字典码获取
         * @param dictType 必填 参数dictType值
         * @param successFn 必填 调用成功回调方法
         */
        initDictByUrl:function (url,dictType,successFn) {
            baseutil.post(url,{
                dictType : dictType
            },function (result) {
                if(0==result.code){
                    //成功
                    successFn(result.data);
                }else{
                    top.layer.msg(result.msg);
                }
            });
        },
        /**********************************************************************************/


        initDictA: function (params,successFn) {
            baseutil.post("/sysAppModule/getApplyList", {
                dictTypes: params
            }, function (result) {
                if (0 == result.code) {
                    //成功
                    successFn(result.data);
                } else {
                    top.layer.msg(result.msg);
                }
            });
        },

        initDictV: function (params,successFn) {
            baseutil.post("/baseVersion/getAllVersion", {
                dictTypes: params
            }, function (result) {
                if (0 == result.code) {
                    //成功
                    successFn(result.data);
                } else {
                    top.layer.msg(result.msg);
                }
            });
        },

        showDictother: function (obj, dataList, showTitle, showValue) {
            if (showTitle != null && showTitle != "" && showTitle != "undefined") {
                obj.append("<option value=''>" + showTitle + "</option>");
            }
            for (var i = 0; i < dataList.length; i++) {
                if(obj.selector == "#applyName"){
                    if (showValue != null && showValue != "" && showValue != "undefined") {
                        if (showValue == dataList[i].name) {
                            obj.append("<option selected value='" + dataList[i].id + "'>" + dataList[i].name + "</option>");
                        } else {
                            obj.append("<option value='" + dataList[i].id + "'>" + dataList[i].name + "</option>");
                        }
                    } else {
                        obj.append("<option value='" + dataList[i].id + "'>" + dataList[i].name + "</option>");
                    }
                }else if(obj.selector == "#Version"){

                    if (showValue != null && showValue != "" && showValue != "undefined") {
                        if (showValue == dataList[i].versionNumber) {
                            obj.append("<option selected value='" + dataList[i].id + "'>" + dataList[i].versionNumber + "</option>");
                        } else {
                            obj.append("<option value='" + dataList[i].id + "'>" + dataList[i].versionNumber + "</option>");
                        }
                    } else {
                        obj.append("<option value='" + dataList[i].id + "'>" + dataList[i].versionNumber + "</option>");
                    }
                }else{
                    if (showValue != null && showValue != "" && showValue != "undefined") {
                        if (showValue == dataList[i].value) {
                            obj.append("<option selected value='" + dataList[i].value + "'>" + dataList[i].name + "</option>");
                        } else {
                            obj.append("<option value='" + dataList[i].value + "'>" + dataList[i].name + "</option>");
                        }
                    } else {
                        obj.append("<option value='" + dataList[i].value + "'>" + dataList[i].name + "</option>");
                    }
                }
            }
            form.render();
        },



        /*******************************************************************************/


        /**
         * @Author: YuShengQiang
         * @Date: 2018-11-27 11:16
         * @Description:字典码下拉框展示
         * @param obj 必填 下拉框对象
         * @param dataList 必填 展示对象
         * @param showTitle 非必填 第一行展示题目（如，全选，请选择）
         * @param showValue 非必填 默认选择值
         */
        showDict: function (obj, dataList, showTitle, showValue) {
            if (showTitle != null && showTitle != "" && showTitle != "undefined") {
                obj.append("<option value=''>" + showTitle + "</option>");
            }
            for (var i = 0; i < dataList.length; i++) {
                if (showValue != null && showValue != "" && showValue != "undefined") {
                    if (showValue == dataList[i].value) {
                        obj.append("<option selected value='" + dataList[i].value + "'>" + dataList[i].name + "</option>");
                    } else {
                        obj.append("<option value='" + dataList[i].value + "'>" + dataList[i].name + "</option>");
                    }
                } else {
                    obj.append("<option value='" + dataList[i].value + "'>" + dataList[i].name + "</option>");
                }

            }
            form.render();
        },
        /**
         * @Author: YuShengQiang
         * @Date: 2018-11-27 11:16
         * @Description:获取字典码值匹配的名称
         * @param dictType 必填 所需字典码code
         * @param dictData 必填 字典码集合
         * @param value 必填 所需字典码值
         */
        aceeptDictContentName: function (dictData, dictType, value) {
            for (var i = 0; i < dictData.length; i++) {
                var obj = dictData[i];
                if (obj.dictType == dictType && obj.value == value) {
                    return obj.name;
                }
            }
        },
        /**
         * @Author: YuShengQiang
         * @Date: 2018-11-27 11:16
         * @Description:获取指定字典码集合
         * @param dictType 必填 所需字典码code
         * @param dictData 必填 字典码集合
         */
        aceeptDictContentList: function (dictData, dictType) {
            var resultDate = [];
            for (var i = 0; i < dictData.length; i++) {
                var obj = dictData[i];
                if(dictType==1){
                    if (obj.removeTag == dictType) {
                        resultDate.push(obj);
                    }
                }else{
                    if (obj.dictType == dictType) {
                        resultDate.push(obj);
                    }
                }
            }
            return resultDate;
        },
        /**
         * @Author: YuShengQiang
         * @Date: 2018-11-27 11:16
         * @Description:工具栏搜索
         * @param params 必填 参数，json格式
         */
        seacher: function (params) {
            table.reload("entityList", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: params
            })
        },
        /**
         * @Author: YuShengQiang
         * @Date: 2018-11-27 11:16
         * @Description:删除功能
         * @param url 必填 删除URL
         * @param title 必填 提示标题
         * @param params 必填 参数，json格式
         * @param successFn 必填 成功回调
         */
        delete: function (url, params, title, successFn) {
            var showTitle="您确定要删除选中项";
            if(title!=null&&title!=""&&title!="undefined"){
                showTitle=title;
            }
            dialog.confirm(showTitle,function () {
                    baseutil.get(url, params, function (result) {
                        if (0 == result.code) {
                            //操作成功
                            successFn(result.data);
                        } else {
                            top.layer.msg(result.msg);
                        }
                    });
                });
        },
        /**
         * @Author: dingxu
         * @Date: 2019-04-09 11:16
         * @Description:列表操作数据
         * @param url 必填 操作URL
         * @param title 必填 提示标题
         * @param params 必填 参数，json格式
         * @param successFn 必填 成功回调
         */
        editlist: function (url, params, title, successFn) {
            dialog.confirm(title,function () {
                baseutil.get(url, params, function (result) {
                    if (0 == result.code) {
                        //操作成功
                        successFn(result.data);
                    } else {
                        top.layer.msg(result.msg);
                    }
                });
            });
        },



        /**
         * @Author: YuShengQiang
         * @Date: 2018-11-27 11:16
         * @Description:编辑功能
         * @param url 必填 删除URL
         * @param id 必填 tabId
         * @param tabName 必填 tab名称
         */
        editPopup: function (url, name, width, height) {
            if (validUtil.isEmpty(width)) {
                width = "700px";
            };
            if (validUtil.isEmpty(height)) {
                height = "620px";
            };
            dialog.popUpPage(name,url,width,height, function (layer, index) {
                var body = layer.getChildFrame('body', index);
                body.find("#save").bind("click", function () {
                    parent.layer.closeAll();
                    location.reload();
                });
                body.find("#cancel").bind("click", function () {
                    parent.layer.closeAll();
                });
            });
        },
        /**
         * @Author: YuShengQiang
         * @Date: 2018-11-27 11:16
         * @Description:弹窗编辑功能
         * @param url 必填 删除URL
         * @param tabName 必填 弹窗名称
         */
        edit: function (id, url, tabName) {
            parent.tab(id, url, tabName);
        },
        /**
         * @Author: YuShengQiang
         * @Date: 2018-11-27 11:16
         * @Description:获取table选中复选框id集合
         * @param url 必填 删除URL
         * @param title 必填 提示标题
         * @param params 必填 参数，json格式
         * @param successFn 必填 成功回调
         */
        checkBoxCheckedIds: function () {
            var checkStatus = table.checkStatus('entityList')
                , data = checkStatus.data;
            var ids = [];
            if (data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    var obj = data[i];
                    ids.push(obj.id);
                }
                return ids.join(",");
            } else {
                top.layer.msg("请先选择对象");
                return null;
            }
        },
        checkBoxCheckedIdAndState: function () {
            var checkStatus = table.checkStatus('entityList');
            var data = checkStatus.data;
            var ids = [];
            var states = [];
            var result = {};
            if (data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    var obj = data[i];
                    ids.push(obj.id);
                    states.push(obj.state);
                }
                result.id = ids.join(",");
                result.state = states.join(",");
                return result;
            } else {
                top.layer.msg("请先选择对象");
                return null;
            }
        },
        checkBoxCheckedIdAndProjectType: function () {
            var checkStatus = table.checkStatus('entityList');
            var data = checkStatus.data;
            var ids = [];
            var projectTypes = [];
            var result = {};
            if (data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    var obj = data[i];
                    ids.push(obj.id);
                    projectTypes.push(obj.projectType);
                }
                result.id = ids.join(",");
                result.projectType = projectTypes.join(",");
                return result;
            } else {
                top.layer.msg("请先选择对象");
                return null;
            }
        },
        checkBoxCheckedIdAndBudget: function () {
            var checkStatus = table.checkStatus('entityList');
            var data = checkStatus.data;
            var ids = [];
            var budgets = [];
            var result = {};
            if (data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    var obj = data[i];
                    ids.push(obj.id);
                    budgets.push(obj.budget);
                }
                result.id = ids.join(",");
                result.budget = budgets.join(",");
                return result;
            } else {
                top.layer.msg("请先选择对象");
                return null;
            }
        }
    }

    //输出baseutil接口
    exports('page', page);
});



